package android.FiltrosJava;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

public class Collage extends AbstractImageBasic {
	protected int incr;
	public Collage(int window){
		setCollage(window);				
	}
	public void setCollage(int window){
		incr = window;
	}

	public BufferedImage applyFilter(BufferedImage src){
    	BufferedImage subImage = null;
    	double ang;
    	int dimX = src.getWidth();
    	int dimY = src.getHeight();
    	int x, y, px, py;
    	BufferedImage pback = new BufferedImage( dimX, dimY, BufferedImage.TYPE_INT_RGB);
		Graphics2D gback = pback.createGraphics();
		gback.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		gback.setColor(Color.white);
		gback.fillRect(0, 0, dimX, dimY);
    	gback.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
    	
    	RotateTransform rot = new RotateTransform(15);
    	    	
    	y = 0;
    	while (y < dimY){
    		x = 0;
    		while (x < dimX){
    			px = incr;
    			py = incr;
    			if ((x + px -1) >= dimX)
    				px = dimX - x;
    			
    			if ((y + py -1) >= dimY)
    				py = dimY - y;
    			
    			subImage = src.getSubimage(x, y, px, py);
    			ang = (float) (40 * Math.random() - 20);
    			rot.setRotation(ang);
    	    	subImage = rot.applyFilter(subImage);
    	    	gback.drawImage(subImage, x, y, null);
    			x += incr;
    		}
    		y += incr;
    	}
	
    	gback.dispose();
		return pback;
    }    
}
